package com.wyj.chat.websocket_chat.controller;

import com.wyj.chat.websocket_chat.pojo.Information;
import com.wyj.chat.websocket_chat.utils.Msg;
import com.wyj.chat.websocket_chat.pojo.User;
import com.wyj.chat.websocket_chat.service.InformationService;
import com.wyj.chat.websocket_chat.service.UserService;
import com.wyj.chat.websocket_chat.utils.InformationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.UUID;


@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private InformationService informationService;

    // 不用输入任何路径直接跳转到登录页面
    @RequestMapping(value = "/")
    public String userToSignIn() {
        return "/signin";
    }

    // 跳转注册页面
    @RequestMapping(value = "/user/signup")
    public String userSignUp() {
        return "/signup";
    }

    // 跳转登录页面
    @RequestMapping(value = "/user/signin")
    public String userSignIn() {
        return "/signin";
    }

    // 登录功能
    @RequestMapping(value = "/user/login", method = RequestMethod.POST)
    public String userLogin(Model model, User user) throws IOException {
        if (StringUtils.isEmpty(user.getName()) || StringUtils.isEmpty(user.getPassWord())) {
            // 登录失败
            model.addAttribute("loginState","fail");
            return "/signin";
        }
        User userLogin = userService.userLogin(user);
        if (userLogin != null) {
            model.addAttribute("user",userLogin);
            // 登录成功
            return "/index";
        }
        // 登录失败
        model.addAttribute("loginState","fail");
        return "/signin";
    }

    // 验证密码
    @RequestMapping(value = "/user/surePass", method = RequestMethod.POST)
    @ResponseBody
    public Msg surePass(String name,String password) throws IOException {
        if (StringUtils.isEmpty(name) || StringUtils.isEmpty(password)) {
            // 登录失败
            return null;
        }
        User user = userService.surePass(name,password);
        Msg msg = new Msg();
        if(user == null) {
            msg.setState("error");
            return msg;
        }
        msg.setState("success");
        return msg;
    }

    // 注册功能
    @RequestMapping(value = "/user/register", method = RequestMethod.POST)
    public String userRegister(Model model,User user) throws IOException {
        // 初始化用户信息
        user.setId(UUID.randomUUID().toString());
        Information information = InformationUtils.initInformation();
        information.setId(user.getId());
        user.setInformation(information);
        informationService.insertInformation(information);
        int flag = userService.insertUser(user);

        if(flag == 1) {
            // 注册成功
            return "/signin";
        }
        // 注册失败
        model.addAttribute("registerState","fail");
        return "/signup";
    }

    @RequestMapping(value = "/user/signIn", method = RequestMethod.POST)
    @ResponseBody
    public boolean signIn(User user) {
        int i = userService.insertUser(user);
        if (i == 0) {
            return false;
        } else {
            return true;
        }
    }

    @RequestMapping(value = "/user/updatePassWord", method = RequestMethod.POST)
    @ResponseBody
    public boolean updatePassWord(String name,String passWord) {
        int i = userService.updatePassWordByName(name,passWord);
        if (i == 0) {
            return false;
        } else {
            return true;
        }
    }

    @RequestMapping(value = "/user/getUserByName", method = RequestMethod.POST)
    @ResponseBody
    public Msg getUserByName(String name) {
        if (StringUtils.isEmpty(name)) {
            return null;
        }
        User user = userService.getByName(name);
        if (user != null) {
            Msg msg = new Msg();
            msg.setState("success");
            msg.setUser(user);
            return msg;
        }else {
            Msg msg = new Msg();
            msg.setState("error");
            msg.setUser(null);
            return msg;
        }
    }
}
